package com.uwantsoft.business.service.domain.city;

import com.uwantsoft.business.facade.dto.city.CityDetailDto;
import com.uwantsoft.business.facade.dto.city.CityDto;
import com.uwantsoft.business.facade.dto.city.CityFormDto;
import com.uwantsoft.business.facade.dto.city.CityOverViewDto;
import com.uwantsoft.util.AbstractEntity;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Configurable;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.math.BigDecimal;

/**
 * @author Chuanbao
 */
@Entity
@Table(name = "city")
@Configurable(autowire = Autowire.BY_NAME)
public class City extends AbstractEntity{
    @Column(name = "name")
    private String name;
    @Column(name = "code")
    private String code;
    @Embedded
    private ParkSortingParameters parkSortingParameters;

    public String name() {
        return name;
    }

    public String code() {
        return code;
    }

    public CityDto convert() {
        CityDto cityDto = new CityDto();
        cityDto.setName(this.name());
        cityDto.setGuid(this.guid());
        return cityDto;
    }

    public static City testInsance() {
        City city = new City();
        city.name = "成都";
        city.code = "CD";
        ParkSortingParameters testParkSortingParameters = new ParkSortingParameters();
        testParkSortingParameters.update(new BigDecimal(11), 11, new BigDecimal(11), new BigDecimal(12));
        return city;
    }

    public CityOverViewDto convertOverView() {
        CityOverViewDto cityOverViewDto = new CityOverViewDto();
        cityOverViewDto.setGuid(this.guid());
        cityOverViewDto.setName(this.name());
        cityOverViewDto.setCode(this.code());
        return cityOverViewDto;
    }


    public static City create(CityFormDto cityFormDto) {
        City city = new City();
        city.name = cityFormDto.getName();
        city.code = cityFormDto.getCode();
        ParkSortingParameters parameters = new ParkSortingParameters();
        parameters.update(new BigDecimal(cityFormDto.getPredefinedPrice()),Integer.parseInt(cityFormDto.getPredefinedDistance()),new BigDecimal(cityFormDto.getPriceWeight()),new BigDecimal(cityFormDto.getDistanceWeight()));
        city.parkSortingParameters = parameters;
        return city;
    }

    public void update(CityFormDto cityFormDto) {
        this.name = cityFormDto.getName();
        this.code = cityFormDto.getCode();
        this.parkSortingParameters.update(new BigDecimal(cityFormDto.getPredefinedPrice()),Integer.parseInt(cityFormDto.getPredefinedDistance()),new BigDecimal(cityFormDto.getPriceWeight()),new BigDecimal(cityFormDto.getDistanceWeight()));
    }

    public CityDetailDto convertDetail() {
        CityDetailDto cityDetailDto = new CityDetailDto();
        cityDetailDto.setGuid(this.guid());
        cityDetailDto.setName(this.name());
        cityDetailDto.setCode(this.code());
        cityDetailDto.setDistanceWeight(this.parkSortingParameters.distanceWeightAsText());
        cityDetailDto.setPriceWeight(this.parkSortingParameters.priceWeightAsText());
        cityDetailDto.setPredefinedDistance(this.parkSortingParameters.predefinedDistanceAsText());
        cityDetailDto.setPredefinedPrice(this.parkSortingParameters.predefinedPriceAsText());
        return cityDetailDto;
    }

    public CityFormDto convertCityForm() {
        CityFormDto cityFormDto = new CityFormDto();
        cityFormDto.setName(this.name());
        cityFormDto.setGuid(this.guid());
        cityFormDto.setCode(this.code());
        cityFormDto.setDistanceWeight(this.parkSortingParameters.distanceWeightAsText());
        cityFormDto.setPriceWeight(this.parkSortingParameters.priceWeightAsText());
        cityFormDto.setPredefinedDistance(this.parkSortingParameters.predefinedDistanceAsText());
        cityFormDto.setPredefinedPrice(this.parkSortingParameters.predefinedPriceAsText());
        return cityFormDto;
    }
}
